var app = getApp();
var ux = require('../../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jname:"",
    jd:[],
    open:false,
    sum:0,
    banrdId:0,
    start:'请选择',
    end: '请选择',
    merchant: null,
    next: true,
    list: [],
    page: 0,
    size: 20,
    actions: "/api/memberUser/getJeDaoLists.do",
    action: "/api/memberUser/hehuorenList.do",
  },
  dclose: function() {
    this.setData({
      open: false
    })
  },
  openJD:function(){
   this.setData({
     open:true
   })
  },
  quanbu:function(){
    this.setData({
      jname: "",
      open: false
    })
  },
  xitem: function (event) {
    var dataset = event.currentTarget.dataset;
    var names=dataset.name;
    this.setData({
      jname:names,
      open:false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      banrdId: wx.getStorageSync("banrdId")
    })
    this.getList("","","");
    this.getJD();
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getJD:function(){
    var self=this;
    ux.request(this.data.actions, {}, function (data) {
        self.setData({
          jd: data.data.jd
        })
    });
  },
  getList: function (start,end,jname) {
    var self = this;
    if (self.data.next == false) {
      wx.showToast({
        title: '没有更多了',
        icon: 'loading',
        duration: 1500
      });
      return;
    }
    wx.showLoading({
      mask: true,
      title: '加载中....'
    })
    var params = { banrdId: self.data.banrdId, page: parseInt(self.data.page), size: self.data.size };
    if (start != "请选择" && start !=""){
       params.start=start+" 00:00:59";
     }
    if (end != "请选择" && start != ""){
       params.end = end+" 23:59:59";
     }
    if (jname) {
      params.jname = jname;
    }
     console.log(JSON.stringify(params))
    ux.request(self.data.action, params, function (data) {
      console.log(JSON.stringify(data))
      var datas = data.data.list;
      if (data.data.pageInfo.next == true) {
        self.setData({
          next: true
        });
      } else {
        self.setData({
          next: false
        });
      }
      if (self.data.page == 0) {
        self.setData({
          list: datas
        });
      } else {
        self.setData({
          list: self.data.list.concat(datas)
        });
      }
      var olist=self.data.list;
      var sum=0;
      for (var i = 0; i < olist.length; i++) {
        sum = ux.acAdd(sum, olist[i].tbpiMoney);
      }
      var pages = self.data.page + 1;
      self.setData({
        sum:sum,
        page: pages
      });
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var start = null;
    if (this.data.start == '请选择') {
    }else{
      start = this.data.start;
    }
    var end = null;
    if (this.data.end == '请选择') {
    }else{
      end = this.data.end;
    }
    this.getList(start,end);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindEndDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      start: e.detail.value
    })
  },
  bindStartDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      end: e.detail.value
    })
  },
  search:function(){
    this.setData({
      next: true,
      list: [],
      page: 0,
    })
    this.getList(this.data.start, this.data.end, this.data.jname);
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  },
})